package com.wedo.mall.activity.chat;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;

import com.e.library.activity.EActivity;
import com.e.library.entity.User;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.hyphenate.easeui.widget.chatrow.EaseChatShareLocation;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;
import com.wedo.mall.R;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.entity.Agent;

/**
 * 咨询商家
 * Created by e on 2017/8/7.
 */

public class ConsultActivity extends EActivity{

    private EaseChatFragment chatFragment;
    String toChatUsername;

    public static void toConsult(EActivity activity, Agent agent){

        User user = new User();
        user.headimgurl = agent.logo;
        user.username = agent.agent_name;
        user.id = agent.user_id;
        toConsult(activity,user,agent.agent_phone);
    }

    public static void toConsult(EActivity activity, User user,String phone){

        String id = user.id;
        if (TextUtils.isEmpty(id)){
            activity.toast("对方账号异常，无法咨询");
            return;
        }
        if (!id.startsWith("wedo")){
            id = "wedo"+user.id;
        }

        Bundle bundle = new Bundle();
        bundle.putParcelable("receiver",user);
        bundle.putString(EaseConstant.EXTRA_PHONE,phone);
        bundle.putString(EaseConstant.EXTRA_USER_ID,id);
        bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE,EaseConstant.CHATTYPE_SINGLE);
        activity.toActivity(ConsultActivity.class,bundle);

    }

    @Override
    protected int layout() {
        return R.layout.activity_consult;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        toChatUsername = getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID);
        Log.i(EaseConstant.TAG,"toChatUsername:"+toChatUsername);
        chatFragment = new EaseChatFragment();
        chatFragment.setChatFragmentHelper(new WedoHelper());

        User user = WLogin.user();

        chatFragment.setSender(WLogin.user());
        Bundle bundle = getIntent().getExtras();
        bundle.putParcelable("sender",user);
        chatFragment.setArguments(bundle);
        getSupportFragmentManager().beginTransaction().add(R.id.fl_fragment, chatFragment).commit();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        // enter to chat activity when click notification bar, here make sure only one chat activity
        String username = intent.getStringExtra("userId");
        if (toChatUsername.equals(username))
            super.onNewIntent(intent);
        else {
            finish();
            startActivity(intent);
        }

    }
    @Override
    public void onBackPressed() {
        chatFragment.onBackPressed();
    }


    private class WedoHelper implements EaseChatFragment.EaseChatFragmentHelper{
        @Override
        public void onSetMessageAttributes(EMMessage message) {
        }

        @Override
        public void onEnterToChatDetails() {
        }

        @Override
        public void onAvatarClick(String username) {
        }

        @Override
        public void onAvatarLongClick(String username) {
        }

        @Override
        public boolean onMessageBubbleClick(EMMessage message) {
            return false;
        }

        @Override
        public void onMessageBubbleLongClick(EMMessage message) {
        }

        @Override
        public boolean onExtendMenuItemClick(int itemId, View view) {
            return false;
        }

        @Override
        public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
            return new WedoChatRow();
        }
    }

    private class WedoChatRow implements EaseCustomChatRowProvider{

        private static final int MESSAGE_TYPE_LOCATION_SHARE_START = 1;

        @Override
        public int getCustomChatRowTypeCount() {
            return 1;
        }

        @Override
        public int getCustomChatRowType(EMMessage message) {
            int type = EaseCommonUtils.type(message);
            if (type == EaseConstant.TYPE_SHARE_LOCATION_START){
                return MESSAGE_TYPE_LOCATION_SHARE_START;
            }
            return 0;
        }

        @Override
        public EaseChatRow getCustomChatRow(EMMessage message, int position, BaseAdapter adapter) {
            int type = EaseCommonUtils.type(message);
            if (type == EaseConstant.TYPE_SHARE_LOCATION_START){
                return new EaseChatShareLocation(ConsultActivity.this,message,position,adapter);
            }

            return null;
        }
    }
}
